package ydzhao.weixin.tuisong.util;

/**
 * @author chlong
 * @date 2022/8/12
 */

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import ydzhao.weixin.tuisong.common.UserCom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 更新通知
 */
@Slf4j
public class TellAll {

    public static void update(List<String> list) {
        String openId = "";
        int len = list.size();
        for (int i = 0; i < len; i++) {
            openId = list.get(i);
            //1，配置
            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId(UserCom.APPID);
            wxStorage.setSecret(UserCom.SECRET);
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
            //模版id
            String templateId = "5Ajh6aPdxDM4R9PVr_PiV7PmZTTOQH0qu4a-GYE-4F8";
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)
                    .templateId(templateId)
                    .build();
            //3,每次更新消息在此处修改通知信息即可！
            templateMessage.addData(new WxMpTemplateData("tip", "亲~本次系统更新啦！", "#000000"));
            templateMessage.addData(new WxMpTemplateData("tips", "更新内容：", "#000000"));
            templateMessage.addData(new WxMpTemplateData("one", "1、发送 ‘更新‘ 二字可以更新系统卡片状态；", "#000000"));
            templateMessage.addData(new WxMpTemplateData("two", "2、发送其余文字，如：笑话，小说故事，姓氏等等文字可以查阅相关内容", "#000000"));
            try {
                System.out.println(templateMessage.toJson());
                System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
            } catch (Exception e) {
                System.out.println("推送失败：" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    /**
     * 版本更新通知
     *
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add(UserCom.OPENID_YOU);
        list.add(UserCom.OPENID_ME);
        log.info("list->{}", list);
        TellAll.update(list);
        log.info("发布成功");

    }
}
